extends KinematicBody2D

const PROJECTILE_SPEED = 400
const MOVING_ENEMY = preload("res://Scripts/moving_enemy.gd")

var direction = Vector2(0, 1)
var material = -3
var disabled = false

func _fixed_process(delta):
    var motion = direction * PROJECTILE_SPEED * delta
    move(motion)

    if (has_node("particles")):
        var theta = rad2deg(Vector2(0, -1).angle_to(direction))
        get_node("particles").set_param(0, theta)

    if (is_colliding()):
        var collider = get_collider()
        if (collider extends TileMap):
            var coords = get_pos() / 32 + direction / 4.0
            coords.x = int(coords.x)
            coords.y = int(coords.y)
            var collided_tile = collider.get_cell(coords.x, coords.y)
            if (collided_tile > -1):
                print("Collision with tile at " + str(coords))
                if (collided_tile == material):
                    disable(true)
                    collider.set_cell(coords.x, coords.y, -1)
                else:
                    disable(false)
        elif (collider extends MOVING_ENEMY):
            print("Collision with moving enemy")
            if (collider.armor_type == material):
                disable(true)
                collider.dead = true
            else:
                disable(false)


    if (disabled):
        queue_free()

func disable(destroyed=false):
    if (destroyed):
        get_parent().get_node("player").get_node("mapsounds").play("destroy")
    else:
        get_parent().get_node("player").get_node("mapsounds").play("nodestroy")
    disabled = true
